﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Media;

namespace JustAMinute
{
    class Graphic
    {
        private PictureBox[] pictureBoxes;
        private List<Card> listOfCards;
        private int cardsCount;
        private List<int> temporaryIndexes = new List<int>(2);
        private int currentNumberOfCards;
        public event Func<List<int>, int, bool> CheckCardsEvent;

        public Graphic(List<Card> listOfCards)
        {
            this.cardsCount = listOfCards.Count;
            this.listOfCards = listOfCards;
            pictureBoxes = new PictureBox[cardsCount]; 
        }
        public void Display(Form form)
        {
            int pictureHeight;
            if (listOfCards.Count == 16)
            {
                pictureHeight = 180;
            }
            else
            {
                pictureHeight = 200;
            }

            for (int i = 0; i < pictureBoxes.Length; i++)
            {
                pictureBoxes[i] = new PictureBox();
                pictureBoxes[i].Image = listOfCards[i].Back;
                pictureBoxes[i].Width = 150;
                pictureBoxes[i].Height = pictureHeight;
                pictureBoxes[i].Cursor = Cursors.Hand;

                Locator(i, pictureBoxes[i]);
                pictureBoxes[i].BorderStyle = BorderStyle.FixedSingle;

                var data = new { picture = pictureBoxes[i], index = i };
                Toggle(data, null);
                form.Controls.Add(pictureBoxes[i]);
            }


        }

        public void CheckCards(object sender, EventArgs e)
        {
            currentNumberOfCards = 0;
            bool b = false;

            for (int i = 0; i < pictureBoxes.Length; i++)
            {
                if (pictureBoxes[i].Image == listOfCards[i].Face)
                {
                    temporaryIndexes.Add(i);
                    currentNumberOfCards++;
                    if (currentNumberOfCards == 2)
                    {
                        b = CheckCardsEvent(temporaryIndexes, currentNumberOfCards);
                        break;
                    }
                }
            }

            if (currentNumberOfCards == 2)
            {
                if (b)
                {
                    pictureBoxes[temporaryIndexes[0]].Visible = false;
                    pictureBoxes[temporaryIndexes[1]].Visible = false;
                    SystemData.CardPairSound.Play();
                }

               
                    pictureBoxes[temporaryIndexes[0]].Image = listOfCards[temporaryIndexes[0]].Back;
                    pictureBoxes[temporaryIndexes[1]].Image = listOfCards[temporaryIndexes[1]].Back;
                
            }

            currentNumberOfCards = 0;
            temporaryIndexes.Clear();
        }

        public void SetCardsAble(bool b)
        {
            for (int i = 0; i < pictureBoxes.Length; i++)
            {
                pictureBoxes[i].Enabled = b;
            }
        }

        private void Locator(int index, PictureBox picture)
        {
            if (cardsCount == 12)
            {
                if (index <= 3)
                {
                    picture.Top = 70;
                    picture.Left = 150 + index * (picture.Width + 100);

                }
                else if (index <= 7)
                {
                    picture.Top = 70 + picture.Height + 30;
                    picture.Left = 150 + (index - 3 - 1) * (picture.Width + 100);
                }
                else if (index <= 11)
                {
                    picture.Top = 70 + (2 * (picture.Height + 30));
                    picture.Left = 150 + (index - 7 - 1) * (picture.Width + 100);
                }
            }

            if (cardsCount == 16)
            {
                if (index <= 3)
                {
                    picture.Top = 20;
                    picture.Left = 150 + index * (picture.Width + 100);

                }
                else if (index <= 7)
                {
                    picture.Top = 20 + picture.Height + 30;
                    picture.Left = 150 + (index - 3 - 1) * (picture.Width + 100);
                }
                else if (index <= 11)
                {
                    picture.Top = 20 + (2 * (picture.Height + 30));
                    picture.Left = 150 + (index - 7 - 1) * (picture.Width + 100);
                }
                else if (index <= 15)
                {
                    picture.Top = 20 + (3 * (picture.Height + 30));
                    picture.Left = 150 + (index - 11 - 1) * (picture.Width + 100);
                }
            }
        }
        private void Toggle(dynamic sender, EventArgs e)
        {
            PictureBox pictureBox = (PictureBox)sender.picture;
            int index = (int)sender.index;


            pictureBox.Click += (obj, z) =>
            {
                SystemData.CardSound.Play();

                if (pictureBox.Image == listOfCards[index].Back)
                {
                    pictureBox.Image = listOfCards[index].Face;
                }

                else
                {
                    pictureBox.Image = listOfCards[index].Back;
                }

            };
        }
    }
}
